package com.atguigu.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.yygh.exception.YyghException;
import com.atguigu.yygh.hosp.repository.DepartmentMongoBbRepository;
import com.atguigu.yygh.hosp.service.DepartmentService;
import com.atguigu.yygh.hosp.utils.HttpRequestHelper;
import com.atguigu.yygh.model.hosp.Department;
import com.atguigu.yygh.vo.hosp.DepartmentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author A
 * @version 1.0
 * @description: TODO
 * @date 2025/3/16 15:43
 */
@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Autowired
    private DepartmentMongoBbRepository departmentMongoBbRepository;
    @Override
    public void saveDepartment(HttpServletRequest request) {
        Map<String, Object> map = HttpRequestHelper.switchRequest(request);
        String jsonString = JSONObject.toJSONString(map);
        Department department = JSONObject.parseObject(jsonString, Department.class);
        Department departmentExit =departmentMongoBbRepository.findDepartmentByHoscodeAndDepcode(department.getHoscode(), department.getDepcode());
        if(departmentExit == null){
            // 新增
            department.setCreateTime(new Date());
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
            departmentMongoBbRepository.save(department);
        }else {
            //更新
            department.setUpdateTime(new Date());
            department.setCreateTime(departmentExit.getCreateTime());
            department.setId(departmentExit.getId());
            department.setIsDeleted(0);
            departmentMongoBbRepository.save(department);
        }


    }

    @Override
    public Page<Department> findDepartmentPage(HttpServletRequest request) {
        String hoscode = request.getParameter("hoscode");
        String page = request.getParameter("page");
        String limit = request.getParameter("limit");
        Department department = new Department();
        department.setHoscode(hoscode);
        Example<Department> departmentExample = Example.of(department);
        PageRequest pageRequest = PageRequest.of(Integer.parseInt(page)-1, Integer.parseInt(limit), Sort.Direction.DESC, "createTime");
        Page<Department> page1 = departmentMongoBbRepository.findAll(departmentExample, pageRequest);
        return page1;
    }

    @Override
    public void removeDepartment(HttpServletRequest request) {
        String hoscode = request.getParameter("hoscode");
        String depcode = request.getParameter("depcode");
        Department department = departmentMongoBbRepository.findDepartmentByHoscodeAndDepcode(hoscode, depcode);
        if(department== null){
            throw new YyghException(20001, "该科室不存在");
        }
        departmentMongoBbRepository.deleteById(department.getId());
    }
    //先查出所有科室，再按大科室分组，再按大科室中小科室分组
    @Override
    public List<DepartmentVo> getDeptList(String hoscode) {
        Department department = new Department();
        department.setHoscode(hoscode);
        Example<Department> example = Example.of(department);
        List<Department> departmentList = departmentMongoBbRepository.findAll(example);
        //按大科室分组
        Map<String, List<Department>> collect = departmentList.stream().collect(Collectors.groupingBy(Department::getBigcode));
        List<DepartmentVo> bigDepartmentVoList = new ArrayList<>();
        for (Map.Entry<String, List<Department>> entry : collect.entrySet()) {
            DepartmentVo bigDepartmentVo = new DepartmentVo();
            bigDepartmentVo.setDepcode(entry.getKey());
            bigDepartmentVo.setDepname(entry.getValue().get(0).getBigname());
            List<DepartmentVo> smallDepartmentVoList = new ArrayList<>();
            for (Department bigDepartment : entry.getValue()) {
                DepartmentVo smalldepartmentVo = new DepartmentVo();
                smalldepartmentVo.setDepcode(bigDepartment.getDepcode());
                smalldepartmentVo.setDepname(bigDepartment.getDepname());
                smallDepartmentVoList.add(smalldepartmentVo);
            }
            bigDepartmentVo.setChildren(smallDepartmentVoList);
            bigDepartmentVoList.add(bigDepartmentVo);
        }
        return bigDepartmentVoList;
    }

    @Override
    public String getDepartmentName(String hoscode, String depcode) {
        Department department = departmentMongoBbRepository.findDepartmentByHoscodeAndDepcode(hoscode, depcode);

        return department.getDepname();
    }

    @Override
    public Department getDepartment(String hoscode, String depcode) {
       return departmentMongoBbRepository.findDepartmentByHoscodeAndDepcode(hoscode,depcode);

    }
}
